Repository navigation
Do not record a usage for a function-like macro name without ( - #759
JayToltTech wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
sorry but CI failed and now there are conflicts.. could you please take a look? I have just released 1.10.0 but a 1.10.1 with this fix would be possible.. |
expand() added the use location to the usage list before checking whether a function-like macro is followed by (, so `int F = 1;` with F defined as a function-like macro was reported as a usage of F although nothing is expanded. Record the usage after that check. macroUsageArg4 expected such a usage for `INT64_C;`; it now expects none.
05a5ed3 to
7e57597
Compare
|
Independent review by Claude Fable 5.2 (Anthropic), run at the PR author's request. Line numbers refer to VerdictThe change is correct for its stated aim, with one gap: How it was checked: the full What holds:
IssuesA.
|
| Input | Before | After |
|---|---|---|
#define F(x) 1#if F#endif |
F used at 2:5 |
no usage |
#if F#endif#define F(x) 1 |
F used at 1:5 |
F used at 1:5 |
#define F(x) 1#if 0#elif F || 1#endif |
F used at 3:7 |
no usage |
The same #if F is a usage when F is defined later, but not when it is defined earlier. #ifdef F and defined(F) still count as usages in both cases.
Two ways to resolve it:
- add such names to
maybeUsedMacrosat line 4621, so#if Fstays a usage; or - decide that
#if Fis not a usage, and correct the comment at line 4620.
Either way it needs a test.
B. The preprocessed output changes for one input (lines 2398-2402)
The early return now also precedes expandBuiltin, so this is not purely a usage-list change:
#define __COUNTER__(x) x
__COUNTER__;This used to output 0 ; and now outputs __COUNTER__ ;. The new result is arguably the right one and the input is contrived, but the commit message describes only the usage list changing.
Optional suggestions
- Redundant condition at line 2412. After the early return,
functionLike() && nameTokInst->nextalready impliesnameTokInst->next->op == '(', so that test can go. - Test coverage.
tokenMacro16andmacroUsageArg4cover only a bare name at the top level. These cases also changed and are cheap to add:G(F): no usage ofF.#define H(a) a F a(5)withH(F): exactly one usage ofF(was two).F\n(1): still one usage.#define F() 1withF; F();: one usage.- the
#if Fcases from issue A.
- Downstream effect. cppcheck uses the list for
cppcheck-suppress-macroand writes it to the dump file as<macro-usage>, so dump consumers will see fewer entries. That may be worth a line in the PR description.
In an #if or #elif condition the usage of a defined macro is left to expand(), which no longer records one for a function-like macro name that is not followed by (. So `#if F` was a usage when F was defined after it, but not when F was defined before it. Record such names like undefined ones.
|
Rebased onto master, conflicts are resolved. The CI failure was the windows-11-arm job hitting the CMake non-target-directive error, which #763 already fixed. The one real conflict was with #761. There's also a second commit. The review above found that The workflows are waiting on approval to run. |
Macro::expand()adds the use location tousageListbefore it checks whether a function-like macro is followed by(. So with#define F(x) x, theFinint F = 1;shows up in the macro usage list even though nothing is expanded there.Came up in cppcheck-opensource/cppcheck#9018, where cppcheck uses the usage list to apply
cppcheck-suppress-macroand that phantom usage suppressed a realzerodiv. That PR filters it on the cppcheck side for now.This moves the
usageList.emplace_backbelow the no-arguments check.__COUNTER__still sees its usage before it reads the count, and builtins aren't function-like so they're unaffected.One output change goes with it. The early return now comes before
expandBuiltin, so#define __COUNTER__(x) xfollowed by a bare__COUNTER__;gives__COUNTER__ ;where it used to give0 ;. I think the new output is the right one, but it isn't only the usage list that changes.The second commit fixes the gap the review found in
#if. A defined name there is left forexpand()to record, which it no longer does for a bare function-like name, so#if Fwas a usage whenFwas defined after it but not before. Those names are now recorded like undefined ones.Outside
#if, cppcheck dump files will have one less<macro-usage>entry per bare function-like name.Tests are
tokenMacro16andtokenMacro17, andmacroUsageArg4from #761 now expects no usage forINT64_C;. testrunner and run-tests.py pass on macOS arm64 after the rebase onto master, andrunformatwith uncrustify 0.80.1 makes no changes.I also dropped this branch into
externals/simplecppon the cppcheck-opensource/cppcheck#9018 branch. Its testrunner fails the same two tests it fails with plain simplecpp master,TestPreprocessor::garbageandTestPreprocessor::standard, which come from simplecpp being newer than cppcheck's copy. Nothing new fails.